﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class panel_RZCode : UserControl
    {
        
        public panel_RZCode()
        {
            InitializeComponent();
            LoadData();
            RZCode.PopupMenuShowing += RZCode_PopupMenuShowing;            
        }

        void RZCode_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.rzcodeContext.Show(MousePosition);
        }

        //////////////////////////////////////////////
        /// Load Data to Grid View
        //////////////////////////////////////////////
        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql = "SELECT PlanNo, RZCode, RobotNo, Date, ConsumpTrans, Note, CusDate, CusSheetNo"
                + " FROM [241_RZCode] ORDER BY RZCode, PlanNo";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridRZCode.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("PlanNo", typeof(string));
                newTable.Columns.Add("RZCode", typeof(string));
                newTable.Columns.Add("RobotNo", typeof(string));
                newTable.Columns.Add("Date", typeof(DateTime));
                newTable.Columns.Add("ConsumptionTransfer", typeof(bool));
                newTable.Columns.Add("Note", typeof(string));
                newTable.Columns.Add("CusDate", typeof(DateTime));
                newTable.Columns.Add("CusSheetNo", typeof(string));
                gridRZCode.DataSource = newTable;
            }
            RZCode.Columns[0].Caption = "Export Plan No";
            RZCode.Columns[1].Caption = "RZ Code";
            RZCode.Columns[2].Caption = "Robot No";
            RZCode.Columns[3].Caption = "Date";
            RZCode.Columns[4].Caption = "Consumption Transfer";
            RZCode.Columns[5].Caption = "Note";
            RZCode.Columns[6].Caption = "Customs Date";
            RZCode.Columns[7].Caption = "Customs Sheet No.";
            RZCode.BestFitColumns();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void refreshSelection_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            frm_Update_RZCodeCustomsDate update_form = new frm_Update_RZCodeCustomsDate();
            update_form.ShowDialog();
            LoadData();
        }

        private void customsUpdateSelection_Click(object sender, EventArgs e)
        {
            frm_Update_RZCodeCustomsDate update_form = new frm_Update_RZCodeCustomsDate();
            update_form.ShowDialog();
            LoadData();
        }
    }
}
